/********************************************************************************
*                 Copyright (C) 2011 - Las Venturas Playground                  *
*                      http://sa-mp.nl | http://lvp-iv.com                      *
*                                                                               *
*    This program is free software: you can redistribute it and/or modify       *
*    it under the terms of the GNU General Public License as published by       *
*    the Free Software Foundation, either version 3 of the License, or          *
*    (at your option) any later version.                                        *
*                                                                               *
*    This program is distributed in the hope that it will be useful,            *
*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
*    GNU General Public License for more details.                               *
*                                                                               *
*    You should have received a copy of the GNU General Public License          *
*    along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
*                                                                               *
*********************************************************************************
*                                                                               *
*  	This function loads all minigames from the correct xml files.               *
*                                                                               *
*   Author:                                                                     *
*   Matthias <matthias@sa-mp.nl>                                                *
*                                                                               *
********************************************************************************/

function loadDeathmatchMinigames () 
{
	// Create a new instance of the xml class
	local xmlFile = xml ("Deathmatch/MinigameList.xml");
	
	// Get the files node
    xmlFile.nodeFind ("Files");
    xmlFile.nodeFirstChild ();
       
	// Loop through each file found
    do
    {		
		// Now create a new instance of the xml class, using the file found.
		local DeathmatchMinigame = xml ("Deathmatch/" + xmlFile.nodeAttribute ("Name"));
		
		// Load all minigame settings
		local EntranceFee  = getXmlValue(DeathmatchMinigame, "EntranceFee");
		local MinimumSignups  = getXmlValue(DeathmatchMinigame, "MinimumSignups");
		local MaximumPlayers  = getXmlValue(DeathmatchMinigame, "MaximumPlayers");
		local TimeLimit  = getXmlValue(DeathmatchMinigame, "Timelimit");
		local Name  = getXmlValue(DeathmatchMinigame, "Name");
		local Command  = getXmlValue(DeathmatchMinigame, "Command");
		
		local MinigameDefinition = Minigames.Definition.DM ();
		
		// Load the minigame settings
		MinigameDefinition.setEntranceFee (EntranceFee.tointeger());
		MinigameDefinition.setMinSignups (MinimumSignups.tointeger());
		MinigameDefinition.setMaxPlayers (MaximumPlayers.tointeger());
		MinigameDefinition.setTimeLimit (TimeLimit.tointeger());
		MinigameDefinition.setMinigameName (Name.tostring());
		MinigameDefinition.setCommand (Command.tostring());

		// Find the Locations node
		DeathmatchMinigame.nodeFind ("Locations");
		DeathmatchMinigame.nodeFirstChild ();
		
		// Loop through each location
		do
		{
			// Get the X, Y, Z and heading of that location
			local X = DeathmatchMinigame.nodeAttribute ("X");
			local Y = DeathmatchMinigame.nodeAttribute ("Y");
			local Z = DeathmatchMinigame.nodeAttribute ("Z");
			local Heading = DeathmatchMinigame.nodeAttribute ("Heading");
			
			// Create a spawnpoint
			local SpawnPoint = Minigames.SpawnInfo ([X.tofloat(), Y.tofloat(), Z.tofloat(), Heading.tofloat()]);
			
			// Now find the Weapons node for that location node
			DeathmatchMinigame.nodeFind ("Weapons");
			DeathmatchMinigame.nodeFirstChild ();
			
			// And loop through each Weapon for that location
			do
			{
				local WeaponID = DeathmatchMinigame.nodeAttribute ("ID");
				local Ammo = DeathmatchMinigame.nodeAttribute ("Ammo");
				
				// Add the spawnweapon for that location
				SpawnPoint.setSpawnWeapon (WeaponID.tointeger(), Ammo.tointeger());
			}
			// Load the next weapon node
			while (DeathmatchMinigame.nodeNext ());
			
			// Add the spawn point to the minigame
			MinigameDefinition.addSpawnPoint (SpawnPoint);
						
			// Set the node parent
			DeathmatchMinigame.nodeParent();
			DeathmatchMinigame.nodeParent();
		}
		// Load the next location
		while (DeathmatchMinigame.nodeNext ());
		
		// And now register the minigame
		Minigames.Core.registerMinigame (MinigameDefinition, Minigames.Instance.DM);
	
		log("* Loaded the " + Name + " deathmatch minigame from " + xmlFile.nodeAttribute ("Name"));
    }
	// Now load the next minigame file
    while (xmlFile.nodeNext ());
}
